package com.huishu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huishu.commons.pojo.BasePage;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.goods.mapper.HotWordsMapper;
import com.huishu.goods.pojo.dto.HotWordsDTO;
import com.huishu.goods.pojo.model.HotWordsDO;
import com.huishu.goods.service.HotWordsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author generator@ljj
 * @since 2023-04-14
 */
@Service
public class HotWordsServiceImpl extends ServiceImpl<HotWordsMapper, HotWordsDO> implements HotWordsService {

    @Override
    public Result pageList(BasePage basePage) {
        Page<HotWordsDO> page = new Page<>(basePage.getCurrPage(), basePage.getPageSize());
        LambdaQueryWrapper<HotWordsDO> queryWrapper = Wrappers.lambdaQuery(HotWordsDO.class)
                .like(StringUtils.isNotBlank(basePage.getSearch()), HotWordsDO::getWords, basePage.getSearch())
                .orderByAsc(HotWordsDO::getSort);
        return Result.success(this.baseMapper.selectPage(page, queryWrapper));
    }

    @Transactional
    @Override
    public Result handle(HotWordsDTO hotWordsDTO, int type) {
        List<HotWordsDO> list = new ArrayList<>();
        if (type == 1) {
            HotWordsDO hotWordsDO = BeanUtils.copyProperties(hotWordsDTO, HotWordsDO.class);
            list.add(hotWordsDO);
            if (hotWordsDTO.getSort() == 1) {
                // 首位添加
                List<HotWordsDO> hotWordsDOList = this.list();
                hotWordsDOList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
            } else {
                // 大于等于的都+1
                List<HotWordsDO> hotWordsDOList = this.list(Wrappers.<HotWordsDO>lambdaQuery()
                        .ge(HotWordsDO::getSort, hotWordsDTO.getSort()));
                // 如果添加3 原来的3 4 5 都加1变成 4 5 6
                hotWordsDOList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
            }

        } else if (type == 2) {
            // 删除时 大于自身的都减去1
            // 删除3 原来的4 5 都-1变成 3 4
            LambdaQueryWrapper<HotWordsDO> queryWrapper = Wrappers.lambdaQuery(HotWordsDO.class)
                    .gt(HotWordsDO::getSort, hotWordsDTO.getSort());
            List<HotWordsDO> hotWordsDOList = this.list(queryWrapper);
            hotWordsDOList.forEach(x -> {
                if (!x.getId().equals(hotWordsDTO.getId())) {
                    x.setSort(x.getSort() - 1);
                    list.add(x);
                }
            });
            this.removeById(hotWordsDTO.getId());
        } else {
            // 修改
            HotWordsDO hotWordsDO = getById(hotWordsDTO.getId());
            if (hotWordsDTO.getSort().equals(hotWordsDO.getSort())) {
                list.add(BeanUtils.copyProperties(hotWordsDTO, HotWordsDO.class));
                // 不做处理
            } else if (hotWordsDTO.getSort() <= hotWordsDO.getSort()) {
                List<HotWordsDO> buoyList = this.list(Wrappers.<HotWordsDO>lambdaQuery()
                        .ge(HotWordsDO::getSort, hotWordsDTO.getSort())
                        .lt(HotWordsDO::getSort, hotWordsDO.getSort()));
                buoyList.forEach(x -> {
                    x.setSort(x.getSort() + 1);
                    list.add(x);
                });
                list.add(BeanUtils.copyProperties(hotWordsDTO, HotWordsDO.class));
            } else {
                List<HotWordsDO> buoyList = this.list(Wrappers.<HotWordsDO>lambdaQuery()
                        .gt(HotWordsDO::getSort, hotWordsDO.getSort())
                        .le(HotWordsDO::getSort, hotWordsDTO.getSort()));
                buoyList.forEach(x -> {
                    x.setSort(x.getSort() - 1);
                    list.add(x);
                });
                list.add(BeanUtils.copyProperties(hotWordsDTO, HotWordsDO.class));
            }
        }
        if(CollectionUtils.isEmpty(list) && type ==2){
            return Result.success();
        }
        boolean b = this.saveOrUpdateBatch(list);
        return b ? Result.success() : Result.fail();
    }

    @Transactional
    @Override
    public Result setDefault(Long id) {
        LambdaUpdateWrapper<HotWordsDO> updateWrapper = Wrappers.lambdaUpdate(HotWordsDO.class)
                .set(HotWordsDO::getIsDefault, false)
                .eq(HotWordsDO::getIsDefault, true);
        this.update(updateWrapper);
        LambdaUpdateWrapper<HotWordsDO> uw = Wrappers.lambdaUpdate(HotWordsDO.class)
                .set(HotWordsDO::getIsDefault, true)
                .eq(HotWordsDO::getId, id);
        this.update(uw);
        return Result.success();
    }
}
